home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Magazine / Online / httpproxy / src / httpresolve.c < prev    next >
C/C++ Source or Header  |  1996-08-20  |  5KB  |  243 lines

  1. /*(( "Header" */
  2. /*
  3.  * $Id: httpresolve.c,v 1.4 1996/08/12 03:33:36 mshopf Exp mshopf $
  4.  *
  5.  * (c) 1995-96 Matthias Hopf
  6.  *
  7.  * A cache file name resolver for HttpProxy.
  8.  */
  9.  
  10. /*
  11.  * $Log: httpresolve.c,v $
  12.  * Revision 1.4  1996/08/12  03:33:36  mshopf
  13.  * SAVE option added. possiblity to write stdin into cache file.
  14.  *
  15.  * Revision 1.3  1996/08/11  22:25:15  mshopf
  16.  * reworked debug messages.
  17.  *
  18.  * Revision 1.2  1996/07/30  13:57:03  mshopf
  19.  * new version switch.
  20.  * standard version string added.
  21.  *
  22.  * Revision 1.1  1996/07/17  16:42:42  mshopf
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27.  
  28. /*)) */
  29. /*(( "Includes & Konstanten" */
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #include <stdarg.h>
  37.  
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40.  
  41. #include "httpproxy.h"
  42. #include "cache.h"
  43. #include "httpproxy_rev.h"
  44.  
  45. #define MAX_COPYBUF (50 * 1024)
  46.  
  47. /*)) */
  48. /*(( "Global Variables" */
  49.  
  50. long   __oslibversion      = 37;
  51. const  char *Version       = "\0$VER: httpresolve 1.0 " __AMIGADATE__ "cache " CACHE_VERSION;
  52.  
  53. #ifdef DEBUG
  54. int DebugLevel = -1;
  55. #endif
  56.  
  57. static FILE *OutStream     = stdout;
  58. static struct RDArgs *RDArgs = NULL;
  59.  
  60. static char *Template      = "URL/K,CACHE=FILE/K,TO/K,VERSION=VER/S,SAVE=WRITE/S";
  61. #define arg_url     ((char *) (RetArray[0]))
  62. #define arg_file    ((char *) (RetArray[1]))
  63. #define arg_to      ((char *) (RetArray[2]))
  64. #define arg_ver     ((long)   (RetArray[3]))
  65. #define arg_save    ((long)   (RetArray[4]))
  66.  
  67. /*)) */
  68.  
  69. /*(( "ExitAll () / ExitErr ()" */
  70.  
  71. /* Close everything and return */
  72.  
  73. void ExitAll (int Ret)
  74. {
  75.     if (OutStream && OutStream != stdout)
  76.     fclose (OutStream);
  77.     OutStream = NULL;
  78.     if (RDArgs)
  79.     FreeArgs (RDArgs);
  80.     RDArgs    = NULL;
  81.     exit (Ret);
  82. }
  83.  
  84.  
  85. void ExitErr (const char *Msg)
  86. {
  87.     fprintf (stderr, "%s\n", Msg);
  88.     ExitAll (10);
  89. }
  90.  
  91. /*)) */
  92. /*(( "CopyInCache ()" */
  93.  
  94. /* Copy FILE to Cache */
  95.  
  96. void CopyInCache (FILE *in, cachefile_t *Cache, const char *Url)
  97. {
  98.     char *Data;
  99.     int  num;
  100.  
  101.     if (CacheOpenNew (Cache, Url))
  102.     ExitErr ("cachefile open failed");
  103.     if (! (Data = AllocMem (MAX_COPYBUF, 0)) )
  104.     {
  105.     CacheClose (Cache, FALSE);
  106.     ExitErr ("not enough memory for copy operation");
  107.     }
  108.     while ( (num = fread (Data, 1, MAX_COPYBUF, in)) > 0)
  109.     CacheWrite (Cache, Data, num);
  110.     CacheClose (Cache, TRUE);
  111.     FreeMem (Data, MAX_COPYBUF);
  112. }
  113.  
  114. /*)) */
  115. /*(( "Action ()" */
  116.  
  117. /* Interprete Arguments */
  118. /* RetArray has to be exactly written like this in order to have the arg_* macros working */
  119.  
  120. void Action (long *RetArray)
  121. {
  122.     static cachefile_t Cache;
  123.     char Buffer [MAX_URLBUFFER];
  124.  
  125.     if (arg_url && arg_file)
  126.     ExitErr ("don't specify both URL and CACHE=FILE");
  127.  
  128.     if (! (arg_url || arg_file))
  129.     ExitErr ("please specify exactly one of URL and CACHE=FILE");
  130.  
  131.     if (arg_save && ! arg_url)
  132.     ExitErr ("only use SAVE=WRITE together with URL");
  133.  
  134.     /* - destination file */
  135.     if (arg_to)
  136.     if (! (OutStream = fopen (arg_to, "wb+")))
  137.         ExitErr ("can't open file");
  138.  
  139.     if (arg_url)
  140.     {
  141.     if (! CacheInit ())
  142.         ExitErr ("internal error");
  143.     if (CacheGet (&Cache, arg_url, NULL) == -1)
  144.         ExitErr ("resolve error");
  145.     fprintf (OutStream, Cache.FileName);
  146.     if (arg_save)
  147.         CopyInCache (stdin, &Cache, arg_url);
  148.     CacheClose (&Cache, TRUE);
  149.     CacheExit ();
  150.     }
  151.     else
  152.     {
  153.     if (! CacheResolve (arg_file, Buffer))
  154.         ExitErr ("resolve error");
  155.     fprintf (OutStream, Buffer);
  156.     }
  157. }
  158.  
  159. /*)) */
  160. /*(( "main ()" */
  161.  
  162. /* main function */
  163.  
  164. void main (int argc, char *argv[])
  165. {
  166.     long RetArray[] = { 0, 0, 0, 0, 0 };
  167.     BPTR lock;
  168.  
  169.     /* Get arguments */
  170.     if (! (RDArgs = ReadArgs (Template, RetArray, NULL)))
  171.     {
  172.     fprintf (stderr, "%s\n"
  173.          "\tURL:     Convert URL to cache filename.\n"
  174.          "\tCACHE:   Convert cache filename to URL.\n"
  175.          "\tTO:      Write output on success into this file.\n"
  176.          "\tVERSION: Output cache version number.\n"
  177.          "\tSAVE:    Write stdin to cache file.\n",
  178.          Template);
  179.     ExitAll (10);
  180.     }
  181.     if (arg_ver)
  182.     {
  183.     fprintf (stdout, CACHE_VERSION "\n");
  184.     ExitAll (0);
  185.     }
  186.  
  187.     if (! (lock = Lock (CACHEDIRVALIDFILE, ACCESS_READ)) )
  188.     ExitErr ("please invoke only inside cache directory");
  189.  
  190.     UnLock (lock);
  191.  
  192.     Action (RetArray);
  193.  
  194.     ExitAll (0);
  195. }
  196.  
  197. /*)) */
  198.  
  199. /*(( "assert()" */
  200.  
  201. #ifndef NDEBUG
  202.  
  203. void ASSERT (int x, const char *text, const char *file, const char *func, int line)
  204. {
  205.     if (x)
  206.     return;
  207.     if (! text)
  208.     text = "unknown";
  209.     fprintf (stderr, "assertion (%s) failed in '%s' of '%s' on line %d\n", text, func, file, line);
  210.     ExitAll (20);
  211.     /*NOTREACHED*/
  212. }
  213.  
  214. #endif /* NDEBUG */
  215.  
  216. /*)) */
  217. /*(( "LogErr ()" */
  218.  
  219. /* We use our own (smaller and less mighty) LogErr */
  220.  
  221. void LogErr (request_t *Req, const char *Status, const char *Url, int ErrNo, const char *Reason, ...)
  222. {
  223.     va_list Args;
  224.     char   IoErrTxt [128];
  225.  
  226.     va_start (Args, Reason);
  227.  
  228.     fprintf  (stderr, "Error ");
  229.     vfprintf (stderr, Reason, Args);
  230.  
  231.     if (ErrNo < 0)
  232.     {
  233.     Fault (IoErr (), "", IoErrTxt, 128);
  234.     fprintf (stderr, "%s\n", IoErrTxt);
  235.     }
  236.     else
  237.     fprintf  (stderr, "\n");
  238.  
  239.     va_end (Args);
  240. }
  241.  
  242. /*)) */
  243.